package com.leanyuan.Infor.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leanyuan.Infor.api.Result;
import com.leanyuan.Infor.domain.User;
import com.leanyuan.publi.domain.House;
import com.leanyuan.publi.domain.Userinfo;
import com.leanyuan.publi.dto.UserInfoDto;
import com.leanyuan.publi.service.HouseService;
import com.leanyuan.publi.service.UserinfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/yezhu")
public class YezhuController {

    @Autowired
    UserinfoService userinfoService;

    @Autowired
    HouseService houseService;

    @RequestMapping("/del")
    public Result<Object> del(@RequestBody Map<String,Integer> param){
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",param.get("id"));
        userinfoService.remove(wrapper);
        return Result.ok();
    }
    @RequestMapping("/Info")
    public Result<Object> Info(@RequestBody UserInfoDto userInfoDto){
        System.out.println(userInfoDto);
        Page<Userinfo> page=new Page<Userinfo>(userInfoDto.getPage(),userInfoDto.getPageSize());
        IPage iPage=userinfoService.findAll(userInfoDto,page);
        return Result.ok(iPage);
    }

    @RequestMapping("/add")
    public Result<Object> add(@RequestBody Userinfo userinfo){
        userinfo.setUserinfocondition("0");
        boolean flag=userinfoService.save(userinfo);
        if(flag){
            return Result.ok();
        }
        return Result.error("失败");
    }

    @RequestMapping("/upd")
    public Result<Object> upd(@RequestBody Userinfo userinfo){
        QueryWrapper<Userinfo> wrapper=new QueryWrapper<>();
        wrapper.eq("user_id",userinfo.getUserId());
        boolean flag=userinfoService.update(userinfo,wrapper);
        if(flag){
            return Result.ok();
        }
        return Result.error("修改信息有误");
    }

    @RequestMapping("/houseInfo")
    public Result<Object> find(@RequestBody Map<String,Object> param){
        Integer page=Integer.valueOf(String.valueOf(param.get("page")));
        Integer pagesize=Integer.valueOf(String.valueOf(param.get("pageSize")));
        Integer id = null;
        String username=null;
        String lname = null;
        if(param.get("id")!=null&&param.get("id")!=""){
            id=Integer.valueOf(String.valueOf(param.get("id")));
        }
        if(param.get("username")!=null&&param.get("username")!=""){
            username=String.valueOf(param.get("username"));
        }
        if(param.get("lname")!=null&&param.get("lname")!=""){
            lname=String.valueOf(param.get("lname"));
        }
        IPage<House> iPage=houseService.findAllp2(new Page<House>(page,pagesize),id,lname,username);
        System.out.println(iPage.getRecords());
        return Result.ok(iPage);
    }

    @RequestMapping("yezhu_house")
    public Result<Object> yezhu_house(@RequestBody House house){
        house.setHCondition("已入住");
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("house_id",house.getHouseId());
        boolean flag=houseService.update(house,wrapper);
        if(flag){
            return Result.ok();
        }
        return Result.error("房屋入住失败");
    }

    @RequestMapping("yezhu_select_house")
    public Result<Object> yezhu_select_house(@RequestBody Map<String,Integer> param){
        System.out.println("..................");
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("user_id",param.get("id"));
        List<House> list=houseService.list(wrapper);
        return Result.ok(list);
    }

    @RequestMapping("yezhu_update_house")
    public Result<Object> yezhu_update_house(@RequestBody Map<String,Integer> param){
        House house=new House();
        house.setHouseId(param.get("id"));
        int i=houseService.upd2(house);
        if(i>0){
            return Result.ok();
        }
        return Result.error("解绑房屋失败");
    }
}
